<?php
namespace clt;
use app\admin\model\Areas;
use app\admin\model\Networks;
use app\admin\model\Pops;
use app\admin\model\CloudItems;
class Price {
	private $data = null;
	private $china = 1; //中國
	private $HK = 32; //香港
	private $lang = '';
    private $coin = '$';
    private $type10 = [124,123];//日韩地区
	public function __construct($data=array(),$lang='zh-tw') {
        $this->data = $data;
        $this->lang = $lang;
        $this->china = Areas::CHINA_ID;
        $this->HK = 32; //香港
    }
    public function getNetworkTotal(){
        /*不支持的线路*/
        if(($this->china != $this->data['country_id'] && $this->HK != $this->data['city_id']) && ($this->china != $this->data['ecountry_id'] && $this->HK != $this->data['ecity_id'])){
            return -2;
        }
    	//DCI 数据中心->数据中心
    	if($this->data['type'] == Networks::NETWORK_TYPE_2 && $this->data['endtype'] == Networks::NETWORK_TYPE_2){ //数据中心->数据中心
    		return $this->dciTodci();
    	}
    	//CCI 云数据->云数据
    	if($this->data['type'] == Networks::NETWORK_TYPE_3 && $this->data['endtype'] == Networks::NETWORK_TYPE_3){
    		return $this->cciTocci();
    	}
        //CCI 数据中心->云数据
        if($this->data['type'] == Networks::NETWORK_TYPE_2 && $this->data['endtype'] == Networks::NETWORK_TYPE_3){
            return $this->dciTocci();
        }
        //CCI 云数据->数据中心
        if($this->data['type'] == Networks::NETWORK_TYPE_3 && $this->data['endtype'] == Networks::NETWORK_TYPE_2){
            return $this->cciTodci();
        }
    } 
    /*DCI To CCI 数据中心->云端*/
    public function dciTocci(){
        /*国内数据中心 - 国内云端*/
        if($this->china == $this->data['country_id'] && $this->china == $this->data['ecountry_id']){

            /*不含AWS*/
            if(!isAWS($this->data['endcloudid'])){
               if($this->data['city_id'] == $this->data['ecity_id']){ //本地费用
                    $map['type'] = 4;
                    return $price1 = $this->getPrice1($map);
                }
               
                if($this->data['city_id'] != $this->data['ecity_id']){ //跨城费用
                    $map['type'] = 5;
                    return $price1 = $this->getPrice1($map);
                }
            }
             /*含AWS*/
             if(isAWS($this->data['endcloudid'])){
                if($this->data['city_id'] == $this->data['ecity_id']){ //本地费用
                    $map['type'] = 6;
                    return $price1 = $this->getPrice1($map);
                }
                if($this->data['city_id'] != $this->data['ecity_id']){ //跨城费用
                    $map['type'] = 7;
                    return $price1 = $this->getPrice1($map);
                } 
            }
            /*国内数据中心到香港云端*/
        }
        /*国内数据中心到国际云端*/
        if($this->china == $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
            /*国内数据中心到香港云端*/
            if($this->data['ecity_id'] == $this->HK){
                $map['type'] = 8;
                $price1 = $this->getPrice1($map); //本地cci费用
                $price2 = $this->getPrice1(['type'=>2]); //跨境费用
                return $price2 + $price1;
            }else{ /*国内数据中心到海外云端*/
                $price1 = 0;
                $price1 = $this->getPrice1(['type'=>2]); //中港跨境费用
                $map['city_id'] = $this->data['ecity_id'];  //海外费用
                $map['type'] = 9;
                $map['city_id'] = $this->data['ecity_id'];  //海外费用
                $map['country_id'] = $this->data['ecountry_id'];
                $map['locationId'] = CloudItems::getLocationId($this->data['ecloud_conter']);
                $price2 = $this->getDCIPrice($map);
                if($price2 == -1){
                    return -1;
                }
                return $price1+$price2;
            }
            
        }
        

        /*海外数据中心->海外云端*/
        if($this->china != $this->data['country_id'] ){

            /*香港数据中心到香港云端*/
            // if($this->data['city_id'] == $this->HK && $this->data['ecity_id'] == $this->HK){
            //     $map['type'] = 8;
            //     return $price1 = $this->getPrice1($map);
            // }
            /*香港数据中心到海外云端*/
            if($this->china != $this->data['ecountry_id']){

                if($this->data['city_id'] == $this->HK ){
                    $map['city_id'] = $this->data['ecity_id'];
                    $map['country_id'] = $this->data['ecountry_id'];
                    $map['locationId'] = CloudItems::getLocationId($this->data['ecloud_conter']);
                    $price2 = $this->getDCIPrice($map);
                    if($price2 === -1){ //请求失败
                        return -1;
                    }
                    return $price2;  
                }
                if($this->data['ecity_id'] == $this->HK ){

                    $map['city_id'] = $this->data['city_id'];
                    $map['country_id'] = $this->data['country_id'];
                    $mageport_locationid = CloudItems::getLocationId($this->data['ecloud_conter']);
                    if(intval($mageport_locationid) <= 0){
                        return 0;
                    }
                    $sleep = $this->data['broadband'];  
                    $megaport = new \clt\Monthly('megaport');
                    $total = $megaport->getPrice($mageport_locationid,$sleep);
                    if($total === false){ //请求失败
                        return -1;
                    }
                    return getCloudFastTotal($total,$this->data['daylength']);  
                }
            }
            
            /*香港数据中心->国内云端*/
            if($this->china == $this->data['ecountry_id']){
                if($this->data['city_id'] == $this->HK ){
                    $map['type'] = 2;
                    $price2 = $this->getPrice1($map); //中港跨境费用
                    if(isAWS($this->data['endcloudid'])){
                        $map['type'] = 6;
                    }else{
                        $map['type'] = 4;
                    }
                    $price1 = $this->getPrice1($map); //本地cci费用
                    return $price1+$price2;
                }
            }

        }
        return false;
    }
    /* CCI To DCI 云端 -> 数据中心*/
    public function cciTodci(){
        /* 国内云端 - 国内数据中心*/
        if($this->china == $this->data['ecountry_id'] && $this->china == $this->data['country_id']){
            /*不含AWS*/
            if(!isAWS($this->data['local_cloud_id'])){
                if($this->data['city_id'] == $this->data['ecity_id']){ //本地费用
                    $map['type'] = 4;
                    return $price1 = $this->getPrice1($map);
                }
                if($this->data['city_id'] != $this->data['ecity_id']){ //跨城费用
                    $map['type'] = 5;

                    return $price1 = $this->getPrice1($map);
                }
            }
             /*含AWS*/
             if(isAWS($this->data['local_cloud_id'])){
                if($this->data['city_id'] == $this->data['ecity_id']){ //本地费用
                    $map['type'] = 6;
                    return $price1 = $this->getPrice1($map);
                }
                if($this->data['city_id'] != $this->data['ecity_id']){ //跨城费用
                    $map['type'] = 7;
                    return $price1 = $this->getPrice1($map);
                }
            }
        }
        /* 国内云端 - 香港数据中心*/
        if($this->china == $this->data['country_id'] && $this->data['ecountry_id'] == Areas::HK_ID){
            /*不含AWS*/
            $price2 = $this->getPrice1(['type'=>2]); //跨境费用
             /*含AWS*/
             if(isAWS($this->data['local_cloud_id'])){
                $map['type'] = 6;
                return $price1 = $this->getPrice1($map)+ $price2;
                
            }else{
                $map['type'] = 4;
                return $price1 = $this->getPrice1($map)+ $price2;
            }
        }
         /*海外云端->国内数据中心*/
         if($this->china != $this->data['country_id'] && $this->china == $this->data['ecountry_id']){
             /*香港云端到国内数据中心*/
            if($this->data['city_id'] == $this->HK){ 
                $map['type'] = 8;
                $price1 = $this->getPrice1($map); //本地cci费用
                $price2 = $this->getPrice1(['type'=>2]); //跨境费用
                return $price2 + $price1;
            }else{ /*国内数据中心到海外云端*/
                $price1 = 0;
                $price1 = $this->getPrice1(['type'=>2]); //中港跨境费用
                $map['city_id'] = $this->data['city_id'];  //海外费用
                $map['country_id'] = $this->data['country_id'];
                $map['locationId'] = CloudItems::getLocationId($this->data['cloud_conter']);
                $price2 = $this->getDCIPrice($map);
                if($price2 === -1){ //请求失败
                    return -1;
                }
                //$map['type'] = 9;
                //$price2 = $this->getPrice1($map); //中港跨境费用
                return $price1+$price2;
            }
         }
         /*海外云端->香港数据中心*/
         if($this->china != $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
             /*海外云端->香港云端到数据中心*/
            if($this->data['city_id'] != $this->HK && $this->data['ecity_id'] == $this->HK ){ 
                $map['country_id'] = $this->data['country_id'];
                $map['city_id'] = $this->data['city_id'];
                $map['locationId'] = CloudItems::getLocationId($this->data['cloud_conter']);
                return $this->getDCIPrice($map); //海外DCI费用
            }
         }
        return false;

    }
    /*CCI 云数据到云数据*/
    public function cciTocci(){
    	/*国内云端*/
    	if($this->china == $this->data['country_id'] && $this->china == $this->data['ecountry_id']){
            
            /*国内云端不含AWS*/
            if(!isAWS($this->data['local_cloud_id']) && !isAWS($this->data['endcloudid'])){
                if($this->data['city_id'] == $this->data['ecity_id']){ //本地云端
                    $map['type'] = 4;
                    $price1 = $this->getPrice1($map);
                    return $price1 > 0 ? $price1*2 : false;
                }
                /*跨城价格*/
                if($this->data['city_id'] != $this->data['ecity_id']){ //本地云端
                    $map['type'] = 4;
                    $price1 = $this->getPrice1($map);
                    $map['type'] = 5;
                    $price2 = $this->getPrice1($map);
                    return $price1+$price2;
                }
            }
            /*本地AWS AWS->其他*/
            if(isAWS($this->data['local_cloud_id']) || isAWS($this->data['endcloudid'])){
                if($this->data['city_id'] == $this->data['ecity_id']){ //本地云端
                    $map['type'] = 6;
                    $price1 = $this->getPrice1($map);
                    $map['type'] = 4;
                    $price2 = $this->getPrice1($map);
                    return $price1+$price2;
                }
                 if($this->data['city_id'] != $this->data['ecity_id']){ //跨城
                    $map['type'] = 7;
                    $price1 = $this->getPrice1($map);
                    $map['type'] = 5;
                    $price2 = $this->getPrice1($map);
                    return $price1+$price2;
                }
            }
    	}
        /*国际云端*/
        if($this->china == $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
            /*国内云 - 海外云*/
            if(isAWS($this->data['local_cloud_id'])){
                $price1 = $this->getPrice1(['type'=>6]);
            }else{
                $price1 = $this->getPrice1(['type'=>4]);
            }
            $price3 = $this->getPrice1(['type'=>2]); //中港跨境费用
            $map['type'] = 9;  //国际其他
            if($this->HK == $this->data['ecity_id']){
                $map['type'] = 8;  //香港
            }
            $map['country_id'] = $this->data['ecountry_id'];
            $map['city_id'] = $this->data['ecity_id'];
            $map['locationId'] = CloudItems::getLocationId($this->data['ecloud_conter']);
            $price2 =  $this->getDCIPrice($map);  //海外DCI费用
            if($price2 == -1){
                return false;
            }
            //return $price2 = $this->getPrice1($map);
            return $price1+$price2+$price3;

        }
        /*海外云端->国内云端*/
        if($this->china != $this->data['country_id'] && $this->china == $this->data['ecountry_id']){
            /*国内云 - 海外云*/
            if(isAWS($this->data['endcloudid'])){
                $price1 = $this->getPrice1(['type'=>6]);
            }else{
                $price1 = $this->getPrice1(['type'=>4]);
            }
            $price3 = $this->getPrice1(['type'=>2]); //中港跨境费用
            $map['type'] = 9;  //国际其他
            if($this->HK == $this->data['city_id']){
                $map['type'] = 8;  //香港
            }
            $map['city_id'] = $this->data['city_id'];
            $map['country_id'] = $this->data['country_id'];
            $map['locationId'] = CloudItems::getLocationId($this->data['cloud_conter']);
            $price2 =  $this->getDCIPrice($map);  //海外DCI费用
            if($price2 == -1){
                return false;
            }
            return $price1+$price2+$price3;

        }
        //海外云端到海外云端
        if($this->china != $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
            /*香港到香港*/
            if($this->HK == $this->data['city_id'] && $this->HK == $this->data['ecity_id']){
                $price1 = $this->getPrice1(['type'=>8]);
                return $price1 ? $price1*2 : false;
            }
            /*香港云端 - 海外云端*/
            if($this->HK == $this->data['city_id'] && $this->HK != $this->data['ecity_id']){
                $map['type'] = 9;
                $map['city_id'] = $this->data['ecity_id'];
                $map['country_id'] = $this->data['ecountry_id'];
                $map['locationId'] = CloudItems::getLocationId($this->data['ecloud_conter']);
                $price1 = $this->getDCIPrice($map);
                if($price1 == -1){
                    return false;
                }
                $price2 = $this->getPrice1(['type'=>8]);
                return $price1 > 0 ? $price1+$price2 : false;
            }
            /*海外云端 - 香港云端 */
            if($this->HK != $this->data['city_id'] && $this->HK == $this->data['ecity_id']){
                $map['type'] = 9;
                $map['city_id'] = $this->data['city_id'];
                $map['country_id'] = $this->data['country_id'];
                $map['locationId'] = CloudItems::getLocationId($this->data['cloud_conter']);
                $price1 = $this->getDCIPrice($map);
                if($price1 == -1){
                    return false;
                }
                $price2 = $this->getPrice1(['type'=>8]);
                return $price1 > 0 ? $price1+$price2 : false;
            }
        }
        return false;
    }
    /*DCI 数据中心到数据中心*/
    public function dciTodci(){
        //国内DCI - 国内DCI
    	if($this->china == $this->data['country_id'] && $this->china == $this->data['ecountry_id']){ //国内DCI
            if($this->data['city_id'] == $this->data['ecity_id']){
                return false ; //同城不提供服务
            }
			$map['type'] = 1;
            return $this->getPrice1($map);
		}
        /*国内DCI - 海外DCI*/
        if($this->china == $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
            //国内到香港
            if($this->HK == $this->data['ecity_id']){ 
                $map['type'] = 2;
                return $this->getPrice1($map);
            }
            //国内到海外
            if($this->HK != $this->data['ecity_id']){ 
                //国内到香港的价格
                $map['type'] = 2;
                $price1 =  $this->getPrice1($map); 
                //香港到国际的价格 
                $map['type'] = 3;
                $map['country_id'] = $this->data['ecountry_id'];
                $map['city_id'] = $this->data['ecity_id'];
                if(!$price1){ //没有该配置
                    return false;
                }
                $map['locationId'] = Pops::getLocationId($this->data['epop_id']);
                $price2 =  $this->getDCIPrice($map);  //海外DCI费用
                if($price2 === -1){
                    return -1;
                }
                return $price1+$price2;//国内到香港的价格+国内到香港的价格
            }
        }
        //海外DCI - 国内DCI
        if($this->china != $this->data['country_id'] && $this->china == $this->data['ecountry_id']){
            //香港到国内
            if($this->HK == $this->data['city_id']){ 
                $map['type'] = 2;
                return $this->getPrice1($map);
            }
            //海外到国内
            if($this->HK != $this->data['city_id']){ 

                //国内到香港的价格
                $map['type'] = 2;
                $price1 =  $this->getPrice1($map); 
                //香港到国际的价格
                $map['type'] = 3;
                $map['country_id'] = $this->data['ecountry_id'];
                $map['city_id'] = $this->data['ecity_id'];
                $map['locationId'] = Pops::getLocationId($this->data['pop_id']);
                $price2 =  $this->getDCIPrice($map);  //海外DCI费用
                if($price2 === -1){
                    return -1;
                }
                if(!$price2){ //没有该配置
                    return false;
                }
                return $price1+$price2;//国内到香港的价格+国内到香港的价格
            }
        }
        /*海外DCI - 海外DCI*/
        if($this->china != $this->data['country_id'] && $this->china != $this->data['ecountry_id']){
            $map['type'] = 3;
            /*香港->国际*/
            if($this->HK == $this->data['city_id'] && $this->HK != $this->data['ecity_id']){ 
                $map['country_id'] = $this->data['ecountry_id'];
                $map['city_id'] = $this->data['ecity_id'];
                $map['locationId'] = Pops::getLocationId($this->data['epop_id']);
                return $this->getDCIPrice($map); //海外DCI费用
            }
            /*国际->香港*/
            if($this->HK != $this->data['city_id'] && $this->HK == $this->data['ecity_id']){ 
                $map['country_id'] = $this->data['country_id'];
                $map['city_id'] = $this->data['city_id'];
                $map['locationId'] = Pops::getLocationId($this->data['pop_id']);
                return $this->getDCIPrice($map); //海外DCI费用
            }

        }
        return false;
    }
    /*國内POP DCI，非香港 数据中心->数据中心*/
    public function getPrice1($map,$total=0){
        $type = isset($map['type']) ? $map['type'] : 0;
        $map['min_mbps'] = array('lt',$this->data['broadband']);
    	$map['max_mbps'] = array('egt',$this->data['broadband']);//$this->getMnps($type);
    	
        //echo db('prices')->getLastSql();
    	
        $rate = 1;
        if($this->data['broadband'] < 10){
             $map['min_mbps'] = array('elt',$this->data['broadband']);
        }
        $priceItem = db('prices')->where($map)->order('max_mbps asc')->find();
        if(empty($priceItem)){
            return false;
        }
    	return $this->getTotal($priceItem)*$rate;
    }
    /*海外DCI费用*/
    public function getDCIPrice($map){
    	// $price = $this->getPrice1($map);
     //    if(!$price){
     //        return false;
     //    }
        //$mageport_locationid = Areas::getArealocation('mageport_locationid',['id'=>$map['city_id']]);
        if(intval($map['locationId']) <= 0){
            return -1;
        }
        $megaport = new \clt\Monthly('megaport');
        $mageport_price = $megaport->getPrice($map['locationId'],$this->data['broadband']);
        if($mageport_price === false){ //请求失败
            return -1;
        }
        // $rate = getRateOFHKD('USD');
        // if(request()->langset() == 'en-us'){ //美元
        //     $mageport_price = round($mageport_price/$rate);
        // }
        return getCloudFastTotal($mageport_price,$this->data['daylength']);
    }
    /*获取金额*/
    public function getTotal($priceItem){
    	$index = intval($this->data['daylength']);
    	if(request()->langset() == 'en-us'){ //美元
    		$price = isset($priceItem['usd'.$index]) ? $priceItem['usd'.$index] : false;
            
    	}else{  //人民币
			$price = isset($priceItem['rmb'.$index]) ? $priceItem['rmb'.$index] : false;
    	}
    	return $price ? $price : false;
    }
    /*获取最大的宽带值*/
    public function getMnps($type=0){
        if(0 < $this->data['broadband'] && $this->data['broadband'] < 10){
            
            return 1;
        }
    	if($this->data['broadband'] == 10){
            
    		return 10;
    	}
    	if(10 < $this->data['broadband'] && $this->data['broadband'] <= 20){
    		return 20;
    	}
    	if(20 < $this->data['broadband'] && $this->data['broadband'] <= 50){
    		return 50;
    	}
    	if(50 < $this->data['broadband'] && $this->data['broadband'] <= 100){
    		return 100;
    	}
    	if(100 < $this->data['broadband'] && $this->data['broadband'] <= 200){
    		return 200;
    	}
    	if(200 < $this->data['broadband'] && $this->data['broadband'] <= 500){
    		return 500;
    	}
    	if(500 < $this->data['broadband'] && $this->data['broadband'] <= 1000){
    		return 1000;
    	}
    }
    /*获取货币*/
    public function getCurrency(){
        if(request()->langset() == 'en-us'){ //美元
            return "USD";
        }
        return "¥";
    }
}